package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptionTypeEmployeeDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.ExceptionType;
import cn.edu.scau.cmi.hombio.domain.ExceptionTypeEmployee;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for ExceptionTypeEmployee entities
 * 
 */

@Service("ExceptionTypeEmployeeService")

@Transactional
public class ExceptionTypeEmployeeServiceImpl implements ExceptionTypeEmployeeService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages ExceptionType entities
	 * 
	 */
	@Autowired
	private ExceptionTypeDAO exceptionTypeDAO;

	/**
	 * DAO injected by Spring that manages ExceptionTypeEmployee entities
	 * 
	 */
	@Autowired
	private ExceptionTypeEmployeeDAO exceptionTypeEmployeeDAO;

	/**
	 * Instantiates a new ExceptionTypeEmployeeServiceImpl.
	 *
	 */
	public ExceptionTypeEmployeeServiceImpl() {
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public ExceptionTypeEmployee saveExceptionTypeEmployeeEmployee(Integer id, Employee related_employee) {
		ExceptionTypeEmployee exceptiontypeemployee = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		exceptiontypeemployee.setEmployee(related_employee);
		related_employee.getExceptionTypeEmployees().add(exceptiontypeemployee);
		exceptiontypeemployee = exceptionTypeEmployeeDAO.store(exceptiontypeemployee);
		exceptionTypeEmployeeDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return exceptiontypeemployee;
	}

	/**
	 * Return a count of all ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public Integer countExceptionTypeEmployees() {
		return ((Long) exceptionTypeEmployeeDAO.createQuerySingleResult("select count(o) from ExceptionTypeEmployee o").getSingleResult()).intValue();
	}

	/**
	 * Return all ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public List<ExceptionTypeEmployee> findAllExceptionTypeEmployees(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<ExceptionTypeEmployee>(exceptionTypeEmployeeDAO.findAllExceptionTypeEmployees(startResult, maxRows));
	}

	/**
	 * Save an existing ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public void saveExceptionTypeEmployee(ExceptionTypeEmployee exceptiontypeemployee) {
		ExceptionTypeEmployee existingExceptionTypeEmployee = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(exceptiontypeemployee.getId());

		if (existingExceptionTypeEmployee != null) {
			if (existingExceptionTypeEmployee != exceptiontypeemployee) {
				existingExceptionTypeEmployee.setId(exceptiontypeemployee.getId());
				existingExceptionTypeEmployee.setIsExceptionManager(exceptiontypeemployee.getIsExceptionManager());
			}
			exceptiontypeemployee = exceptionTypeEmployeeDAO.store(existingExceptionTypeEmployee);
		} else {
			exceptiontypeemployee = exceptionTypeEmployeeDAO.store(exceptiontypeemployee);
		}
		exceptionTypeEmployeeDAO.flush();
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public ExceptionTypeEmployee deleteExceptionTypeEmployeeEmployee(Integer exceptiontypeemployee_id, Integer related_employee_id) {
		ExceptionTypeEmployee exceptiontypeemployee = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(exceptiontypeemployee_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		exceptiontypeemployee.setEmployee(null);
		related_employee.getExceptionTypeEmployees().remove(exceptiontypeemployee);
		exceptiontypeemployee = exceptionTypeEmployeeDAO.store(exceptiontypeemployee);
		exceptionTypeEmployeeDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return exceptiontypeemployee;
	}

	/**
	 * Load an existing ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public Set<ExceptionTypeEmployee> loadExceptionTypeEmployees() {
		return exceptionTypeEmployeeDAO.findAllExceptionTypeEmployees();
	}

	/**
	 * Save an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public ExceptionTypeEmployee saveExceptionTypeEmployeeExceptionType(Integer id, ExceptionType related_exceptiontype) {
		ExceptionTypeEmployee exceptiontypeemployee = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(id, -1, -1);
		ExceptionType existingexceptionType = exceptionTypeDAO.findExceptionTypeByPrimaryKey(related_exceptiontype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingexceptionType != null) {
			existingexceptionType.setId(related_exceptiontype.getId());
			existingexceptionType.setName(related_exceptiontype.getName());
			existingexceptionType.setMemo(related_exceptiontype.getMemo());
			related_exceptiontype = existingexceptionType;
		}

		exceptiontypeemployee.setExceptionType(related_exceptiontype);
		related_exceptiontype.getExceptionTypeEmployees().add(exceptiontypeemployee);
		exceptiontypeemployee = exceptionTypeEmployeeDAO.store(exceptiontypeemployee);
		exceptionTypeEmployeeDAO.flush();

		related_exceptiontype = exceptionTypeDAO.store(related_exceptiontype);
		exceptionTypeDAO.flush();

		return exceptiontypeemployee;
	}

	/**
	 * Delete an existing ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public void deleteExceptionTypeEmployee(ExceptionTypeEmployee exceptiontypeemployee) {
		exceptionTypeEmployeeDAO.remove(exceptiontypeemployee);
		exceptionTypeEmployeeDAO.flush();
	}

	/**
	 * Delete an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public ExceptionTypeEmployee deleteExceptionTypeEmployeeExceptionType(Integer exceptiontypeemployee_id, Integer related_exceptiontype_id) {
		ExceptionTypeEmployee exceptiontypeemployee = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(exceptiontypeemployee_id, -1, -1);
		ExceptionType related_exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(related_exceptiontype_id, -1, -1);

		exceptiontypeemployee.setExceptionType(null);
		related_exceptiontype.getExceptionTypeEmployees().remove(exceptiontypeemployee);
		exceptiontypeemployee = exceptionTypeEmployeeDAO.store(exceptiontypeemployee);
		exceptionTypeEmployeeDAO.flush();

		related_exceptiontype = exceptionTypeDAO.store(related_exceptiontype);
		exceptionTypeDAO.flush();

		exceptionTypeDAO.remove(related_exceptiontype);
		exceptionTypeDAO.flush();

		return exceptiontypeemployee;
	}

	/**
	 */
	@Transactional
	public ExceptionTypeEmployee findExceptionTypeEmployeeByPrimaryKey(Integer id) {
		return exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(id);
	}
}
